package com.jdrx.sw.sewage.api.web;

import com.jdrx.platform.commons.rest.beans.dto.IdDTO;
import com.jdrx.platform.commons.rest.beans.vo.ResponseVO;
import com.jdrx.platform.commons.rest.exception.BizException;
import com.jdrx.platform.commons.rest.factory.ResponseFactory2;
import com.jdrx.sw.sewage.beans.dto.NewsAddDTO;
import com.jdrx.sw.sewage.beans.vo.NewsVO;
import com.jdrx.sw.sewage.common.constant.Constant;
import com.jdrx.sw.sewage.service.NewsServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping(value = "/api/news")
@Api(value = "新闻模块")
public class NewsApi {

    @Autowired
    private NewsServiceImpl newsServiceImpl;

    @ApiOperation(value = "查询新闻列表")
    @RequestMapping(value = "findList", method = RequestMethod.POST)
    public ResponseVO<List<NewsVO>> findMaintenanceDevList(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                                           @RequestHeader(Constant.TENANT_ID) String tenantId) {
        return ResponseFactory2.ok(newsServiceImpl.findList());
    }

    @ApiOperation(value = "新增新闻")
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public ResponseVO add(@RequestHeader(Constant.DATA_NODE) String dataNode,
                          @RequestHeader(Constant.TENANT_ID) String tenantId,
                          @RequestBody @Valid NewsAddDTO newsAddDTO) throws BizException {
        return newsServiceImpl.add(newsAddDTO);
    }

    @ApiOperation(value = "更新新闻")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public ResponseVO update(@RequestHeader(Constant.DATA_NODE) String dataNode,
                             @RequestHeader(Constant.TENANT_ID) String tenantId,
                             @RequestBody @Valid NewsAddDTO newsAddDTO) throws BizException {
        newsServiceImpl.update(newsAddDTO);
        return ResponseFactory2.ok();
    }

    @ApiOperation(value = "删除新闻")
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public ResponseVO delete(@RequestHeader(Constant.DATA_NODE) String dataNode,
                             @RequestHeader(Constant.TENANT_ID) String tenantId,
                             @RequestBody @Valid IdDTO dto) throws BizException {
        newsServiceImpl.delete(dto);
        return ResponseFactory2.ok();
    }
}
